﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataAccess
{
    public class DangNhapHeThong
    {
        private string _tenDangNhap;

        public string TenDangNhap
        {
            get { return _tenDangNhap; }
            set { _tenDangNhap = value; }
        }

        private string _matKhau;
        public string MatKhau
        {
            get { return _matKhau; }
            set { _matKhau = value; }
        }

        private int _quyenHan;
        public int QuyenHan
        {
            get { return _quyenHan; }
            set { _quyenHan = value; }
        }

        public DangNhapHeThong() { }

        public DangNhapHeThong(string ten, string pass, int qu)
        {
            TenDangNhap = ten;
            MatKhau = pass;
            QuyenHan = qu;
        }

        public DangNhapHeThong(DangNhapHeThong dn)
        {
            TenDangNhap = dn.TenDangNhap;
            MatKhau = dn.MatKhau;
            QuyenHan = dn.QuyenHan; 
        }

        public bool LoginHeThong(string TenDangNhap, string MatKhau)
        {
            using (SqlConnection connec=MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("QuanLyNguoiDung_Login", connec);
                cmd.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 50).Value = TenDangNhap;
                cmd.Parameters.Add("@MatKhau", SqlDbType.NVarChar, 50).Value = MatKhau;
                connec.Open();
                using (SqlDataReader reader=cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }                        
        }

        public bool DangNhapAdMin(string ten, string pass)
        {
            using (SqlConnection connec=MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("QuanLyNguoiDung_LogInAdMin", connec);
                cmd.Parameters.Add("@Ten", SqlDbType.NVarChar, 50).Value = ten;
                cmd.Parameters.Add("@pass", SqlDbType.NVarChar, 50).Value = pass;
                connec.Open();
                using (SqlDataReader reader=cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                
                
            }
        }
        public static int QuyenHanHeThong(string TenDangNhap, string MatKhau)
        {

            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("QuanLyNguoiDung_QuyenHan", connec);
                cmd.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 50).Value = TenDangNhap;
                cmd.Parameters.Add("@MatKhau", SqlDbType.NVarChar, 50).Value = MatKhau;
                connec.Open();
                int gt = (int)cmd.ExecuteScalar();
                return gt;
            }

        }

        public static List<DangNhapHeThong> All()
        {
            List<DangNhapHeThong> list = new List<DangNhapHeThong>();
            using (SqlConnection connec=MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("QuanLyNguoiDung_All", connec);
                connec.Open();
                using (SqlDataReader reader=cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        DangNhapHeThong dn = GetGV(reader);
                        list.Add(dn);
                    }
                }
            }
            return list;
        }

        public static int Add(DangNhapHeThong dn)
        {
            using (SqlConnection connec=MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("QuanLyNguoiDung_Add", connec);
                cmd.Parameters.Add("@Ten", SqlDbType.NVarChar, 50).Value = dn.TenDangNhap;
                cmd.Parameters.Add("@Pass", SqlDbType.NVarChar, 50).Value = dn.MatKhau;
                cmd.Parameters.Add("@quyenhan", SqlDbType.Int).Value = dn.QuyenHan;
                connec.Open();
                int num = cmd.ExecuteNonQuery();
                return num;
            }
        }
        public static int Add(string ten,string pass,int qh)
        {
            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("QuanLyNguoiDung_Add", connec);
                cmd.Parameters.Add("@Ten", SqlDbType.NVarChar, 50).Value = ten;
                cmd.Parameters.Add("@Pass", SqlDbType.NVarChar, 50).Value = pass;
                cmd.Parameters.Add("@quyenhan", SqlDbType.Int).Value = qh;
                connec.Open();
                int num = cmd.ExecuteNonQuery();
                return num;
            }
        }

        public static int Delete(DangNhapHeThong dn)
        {
            using (SqlConnection connec = MyConnection.MyConn())
            {
                SqlCommand cmd = MyCommand.Command("QuanLyNguoiDung_Delete", connec);
                cmd.Parameters.Add("@Ten", SqlDbType.NVarChar, 50).Value = dn.TenDangNhap;
                connec.Open();
                int num = cmd.ExecuteNonQuery();
                return num;
            }
        }




        public static int ResetPass(string pass)
        {
            try
            {
                using (SqlConnection connec = MyConnection.MyConn())
                {
                    SqlCommand cmd = MyCommand.Command("QuanLyNguoiDung_ResetPass", connec);
                    cmd.Parameters.Add("@TenDangNhap", SqlDbType.NVarChar, 50).Value = pass;
                    connec.Open();
                    int num = cmd.ExecuteNonQuery();
                    return num;
                }
            }
            catch (Exception)
            {
                return 0;
            }
            
        }

        public static int GVResetPass(string tendn,string pass ,string passnew)
        {
            try
            {
                using (SqlConnection connec= MyConnection.MyConn())
                {
                    SqlCommand cmd = MyCommand.Command("GiaoVien_Resetpassword", connec);
                    cmd.Parameters.Add("@ten", SqlDbType.NVarChar, 50).Value = tendn;
                    cmd.Parameters.Add("@pass", SqlDbType.NVarChar, 50).Value = pass;
                    cmd.Parameters.Add("@passnew", SqlDbType.NVarChar, 50).Value = passnew;
                    connec.Open();
                    int num = cmd.ExecuteNonQuery();
                    return num;
                }
            }
            catch (Exception)
            {

                return 0;
            }
        }
        
        // ham bo tro:
        private static DangNhapHeThong GetGV(SqlDataReader reader)
        {
            DangNhapHeThong dn = new DangNhapHeThong();
            dn.TenDangNhap = reader["TenDangNhap"].ToString();
            dn.MatKhau = reader["MatKhau"].ToString();
            dn.QuyenHan = CType.ToInt32(reader["QuyenHan"]);
            return dn;
        }

    }
}
